package cn.lyx.framework.context.holder;

import cn.lyx.framework.common.constant.GlobalConstants;
import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author by lyx
 */
public class LoginUserContextHolder {
    private static ThreadLocal<Map<String, Object>> LOGIN_USER_CONTEXT_THREAD_LOCAL =
            TransmittableThreadLocal.withInitial(HashMap::new);

    public static void setUserId(Object value) {
        LOGIN_USER_CONTEXT_THREAD_LOCAL.get().put(GlobalConstants.USER_ID, value);
    }

    public static Long getUserId() {
        Object object = LOGIN_USER_CONTEXT_THREAD_LOCAL.get().get(GlobalConstants.USER_ID);
        if (Objects.isNull(object)) {
            return null;
        }
        return Long.valueOf(object.toString());
    }

    public static void remove() {
        LOGIN_USER_CONTEXT_THREAD_LOCAL.remove();
    }
}
